Passed
Push — master ( 73dd7e...447275 )
by Eric
59s
created

cookiejs.js ➔ ... ➔ cookiejs.remove   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 2 Features 1
Metric Value
cc 3
eloc 8
c 3
b 2
f 1
nc 2
nop 2
dl 0
loc 12
rs 10
1
(function(f) {
2
  if (typeof exports === 'object' && typeof module !== 'undefined') {
3
    module.exports = f();
4
  } else if (typeof define === 'function' && define.amd) {
5
    define([], f);
6
  } else {
7
    var g;
8
    if (typeof window !== 'undefined') {
9
      g = window;
10
    } else if (typeof global !== 'undefined') {
11
      g = global;
12
    } else if (typeof self !== 'undefined') {
13
      g = self;
14
    } else {
15
      g = this;
16
    }
17
    g.cookiejs = f();
18
  }
19
})(function() {
20
  var define, module, exports;
21
  module = { exports: (exports = {}) };
22
  /*!
23
 * cookiejs.js | v1.0.0 | cookiejs object for setting/getting/removing cookies
24
 * Copyright (c) 2017 Eric Zieger (MIT license)
25
 * https://github.com/theZieger/cookiejs.js/blob/master/LICENSE
26
 */
27
28
  /**
29
   * throwTypeError
30
   *
31
   * @param {String} sName
32
   * @param {String} sType
33
   *
34
   * @throws {TypeError}
35
   */
36
  var throwTypeError = function(sName, sType) {
37
    throw new TypeError(sName + ' is not of type ' + sType);
38
  };
39
40
  var cookiejs = {
41
    global: this.document ? this.document : { cookie: '' },
42
43
    /**
44
     * sets or overwrites a cookie
45
     *
46
     * @param {String} sCookieName - the name of the cookie you want to set
47
     * @param {String} sValue - the value you want to set
48
     * @param {String} oAttributes - options e.g. domain, path, expires
49
     *
50
     * @throws {TypeError} if argument sCookieName is empty or not a string
51
     */
52
    set: function(sCookieName, sValue, oAttributes) {
53
      var sAttributes = '';
54
55
      sValue = sValue || '';
56
57
      if (!sCookieName || typeof sCookieName !== 'string') {
58
        throwTypeError('sCookieName', 'string');
59
      }
60
61
      if (typeof sValue !== 'string') {
62
        throwTypeError('sValue', 'string');
63
      }
64
65
      if (oAttributes === undefined) {
66
        sAttributes += '; path=/';
67
      } else {
68
        if (typeof oAttributes !== 'object' || Array.isArray(oAttributes)) {
69
          throwTypeError('oAttributes', 'object');
70
        }
71
72
        Object.keys(oAttributes).forEach(function(sAttr) {
73
          if (sAttr === 'secure') {
74
            if (oAttributes[sAttr] === true || oAttributes[sAttr] === 'true') {
75
              sAttributes += ';' + sAttr;
76
              return;
77
            } else {
78
              throwTypeError(sAttr, 'boolean');
79
            }
80
          } else if (typeof oAttributes[sAttr] !== 'string') {
81
            throwTypeError(sAttr, 'string');
82
          }
83
84
          sAttributes += ';' + sAttr + '=' + oAttributes[sAttr];
85
        });
86
      }
87
88
      cookiejs.global.cookie =
89
        encodeURIComponent(sCookieName) +
90
        '=' +
91
        encodeURIComponent(sValue) +
92
        sAttributes;
93
    },
94
95
    /**
96
     * returns the value of a cookie
97
     *
98
     * @param {String} sCookieName
99
     *
100
     * @throws {TypeError}
101
     *
102
     * @returns {String|Boolean}
103
     */
104
    get: function(sCookieName) {
105
      var gCookieValue = false;
106
107
      if (!sCookieName || typeof sCookieName !== 'string') {
108
        throwTypeError('sCookieName', 'string');
109
      }
110
111
      cookiejs.global.cookie.split('; ').some(function(sCookie) {
112
        var aCookie = sCookie.split('=');
113
114
        if (decodeURIComponent(aCookie[0]) === sCookieName) {
115
          gCookieValue = decodeURIComponent(aCookie[1]);
116
          return true;
117
        }
118
119
        return false;
120
      });
121
122
      return gCookieValue;
123
    },
124
125
    /**
126
     * removes a specific cookie
127
     *
128
     * oAttributes must contain the correct path and domain it won't
129
     * remove the cookie
130
     *
131
     * @param {String} sCookieName
132
     * @param {Object} oAttributes - options e.g. domain, path, expires
133
     *
134
     * @throws {TypeError}
135
     */
136
    remove: function(sCookieName, oAttributes) {
137
      var oRemoveAttributes = oAttributes || {};
138
139
      if (
140
        typeof oRemoveAttributes === 'object' &&
141
        !Array.isArray(oRemoveAttributes)
142
      ) {
143
        oRemoveAttributes.expires = 'Thu, 01 Jan 1970 00:00:01 GMT';
144
      }
145
146
      cookiejs.set(sCookieName, '', oRemoveAttributes);
147
    }
148
  };
149
150
  module.exports = cookiejs;
151
152
  return module.exports;
153
});
154